package cn.hy.today;


import datastruct.ListNode;

import java.util.Stack;

class Solution_2487 {
    public ListNode removeNodes(ListNode head) {
        Stack<ListNode> stack = new Stack<>();
        while (head != null) {
            stack.push(head);
            head = head.next;
        }
        while (!stack.isEmpty()) {
            // 低部 是尾部大于 head 值 说明可以连接， 否则是要删除的
            if (head == null || stack.peek().val >= head.val) {
                stack.peek().next = head;
                head = stack.peek();
            }
            stack.pop();
        }
        return head;
    }
}